
package progentiere.knapsackprb;


public class Solution 
{

    private boolean isOptimal;
    private double[] content;

    public Solution(int nbItems)
    {
        this.isOptimal = false;
        this.content = new double[nbItems];
        
        for(int i = 0 ; i < nbItems ; i++)
        {
            this.content[i] = 0;
        }
    }
    
    public Solution(Solution s)
    {
        this.isOptimal = s.isOptimal;
        this.content = new double[s.content.length];
        System.arraycopy(s.content, 0, this.content, 0, s.content.length);
    }
    
    public Solution(double[] content) 
    {
        this.content = content;
    }
    
    public boolean isOptimal() 
    {
        return isOptimal;
    }

    public void setOptimal()
    {
        this.isOptimal = true;
    }
    
    public double[] getContent() 
    {
        return content;
    }
    
    public double getWeight(KnapSackProblem problem)
    {
        double totalWeight = 0;
        
        for (int i = 0 ; i < this.content.length ; i++)
        {
            totalWeight += problem.getItems().get(i).getWeight() * this.content[i];
        }
        
        return totalWeight;
    }
    
    public double getProfit(KnapSackProblem problem)
    {
        double totalProfit = 0;
        
        for (int i = 0 ; i < this.content.length ; i++)
        {
            totalProfit += problem.getItems().get(i).getProfit() * this.content[i];
        }
        
        return totalProfit;
    }
    
    @Override
    public String toString()
    {
        String s = "" + this.isOptimal;
        
        for (int i = 0 ; i < this.content.length ; i++)
        {
            s += " " + this.content[i];
        }
        
        return s;
    }
}
